package com.example.cash.craft.dao.persist.repository.impl;

import com.example.cash.craft.dao.persist.mapper.UserMapper;
import com.example.cash.craft.dao.persist.repository.IUserRepository;
import com.example.cash.craft.pojo.entity.User;
import com.example.cash.craft.pojo.vo.PageData;
import com.example.cash.craft.pojo.vo.UserLoginResultVO;
import com.example.cash.craft.pojo.vo.UserVO;
import com.example.cash.craft.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserRepositoryImpl implements IUserRepository {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int insert(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int deleteById(Long id) {
        return userMapper.deleteById(id);
    }

    @Override
    public int updateById(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int countByUsername(String username) {
        return userMapper.countByUsername(username);
    }

    @Override
    public UserVO selectById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public UserLoginResultVO selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public PageData<UserVO> listAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<UserVO> list = userMapper.listAll();
        PageInfo<UserVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
}
